////////////////////////////////////////////////////////////
//
// SFML GUI - Simple and Fast GUI Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com) (for SFML)
// Copyright (C) 2010 Janniaux Alexandre (alexand69580@yahoo.fr) (for GUI)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

#include "GuiListener.hpp"

namespace gui
{

GuiListener::GuiListener() :

    myFocusWidget(NULL),
    myPressedWidget(NULL),
    myHoveredWidget(NULL)
{

}

void GuiListener::AddWidget(Widget* widget)
{
    myWidgets.push_back(widget);
}

void GuiListener::RemoveWidget(Widget* widget)
{
    myWidgets.remove(widget);
}

void GuiListener::SetFocusWidget(Widget* widget)
{
    myFocusWidget = widget;
}

bool GuiListener::PushEvent(sf::Event& event)
{

    if(event.Type == sf::Event::MouseButtonPressed
    || event.Type == sf::Event::MouseButtonReleased
    || event.Type == sf::Event::MouseWheelMoved
    || event.Type == sf::Event::MouseMoved)
    {
        return ProcessMouseEvent(event);
    }
    else if(event.Type == sf::Event::KeyPressed ||
    event.Type == sf::Event::KeyReleased)
    {
        return ProcessKeyEvent(event);
    }


    return false;
}

void GuiListener::AddEvent(sf::Event& event)
{
    myEvents.push(event);
}

bool GuiListener::GetEvent(sf::Event& event)
{
    if(myEvents.size())
    {
        event = myEvents.front();
        myEvents.pop();
        return true;
    }
    return false;
}

void GuiListener::RenderGui(sf::RenderTarget& target)
{
    std::list<Widget*>::iterator it = myWidgets.begin();
    while(it != myWidgets.end())
    {
        if((*it))
        {
            target.Draw((*(*it)));
            std::list<Widget* >::iterator childit = (*it)->myChildren.begin();
            while(childit != (*it)->myChildren.end())
            {
                target.Draw((*  (*childit)   )) ;
                childit++;
            }

        }

        it++;
    }
}

bool GuiListener::ProcessMouseEvent(sf::Event& event, Widget* base)
{
    std::list<Widget*>::reverse_iterator it;
        if(base)
            it = base->myChildren.rbegin();
        else
            it = myWidgets.rbegin();

    while(it != (base?base->myChildren.rend():myWidgets.rend()))
    {
        if(event.Type == sf::Event::MouseButtonPressed)
        {
            myPressedWidget = NULL;
            std::cout << "Mouse clic : " << event.MouseButton.X << " ; " << event.MouseButton.Y << std::endl;
            if((*it)->AcceptMouseEvent(event.MouseButton.X, event.MouseButton.Y))
            {

                if(!ProcessMouseEvent(event, *it))
                {
                    (*it)->OnMouseButtonPressed(event);

                    if(myFocusWidget)
                        myFocusWidget->OnFocusLeft(event);
                    myFocusWidget = (*it);
                        myFocusWidget->OnFocusEntered(event, Focus::MouseFocus);
                    myPressedWidget = (*it);
                    myPressedEvent = event;
                }
                return true;
            }
        }

        else if(event.Type == sf::Event::MouseButtonReleased)
        {
            if((*it)->AcceptMouseEvent(event.MouseButton.X, event.MouseButton.Y))
            {

                if(!ProcessMouseEvent(event, *it))
                {
                    (*it)->OnMouseButtonReleased(event);
                    if(myPressedWidget == (*it) && myPressedEvent.MouseButton.Button == event.MouseButton.Button)
                    {
                        (*it)->OnMouseClicked(event);
                        myPressedWidget = NULL;
                    }
                    else
                        myPressedWidget = NULL;
                }
                return true;
            }
            else if(myPressedWidget != NULL)
            {
                myPressedWidget->OnMouseButtonReleased(event, false);
                myPressedWidget = NULL;
            }
        }

        else if(event.Type == sf::Event::MouseMoved)
        {
            if((*it)->AcceptMouseEvent(event.MouseMove.X, event.MouseMove.Y))
            {
                if(!ProcessMouseEvent(event, *it))
                {
                    if(myHoveredWidget == (*it))
                        return true;
                    if(myHoveredWidget!=NULL)
                        myHoveredWidget->OnMouseLeft(event);
                    (*it)->OnMouseEntered(event);
                    myHoveredWidget = (*it);
                }
                return true;
            }
            else if(myHoveredWidget)
            {
                myHoveredWidget->OnMouseLeft(event);
                myHoveredWidget = NULL;
            }
        }

        else if(event.Type == sf::Event::MouseWheelMoved)
        {
            if((*it)->GetRect().Contains(event.MouseWheel.X, event.MouseWheel.Y))
            {

                if(ProcessMouseEvent(event, *it))
                    (*it)->OnMouseWheelMoved(event);
                return true;
            }
        }

        it++;
    }

    return false;
}

bool GuiListener::ProcessKeyEvent(sf::Event& event)
{
    if(myFocusWidget)
    {
        if(event.Type == sf::Event::KeyPressed)
        {
            myFocusWidget->OnKeyPressed(event);
            return true;
        }
        else if(event.Type == sf::Event::KeyReleased)
        {
            myFocusWidget->OnKeyReleased(event);
            return true;
        }
        else
            return false;
    }

    return false;

}

} // namespace gui
